package com.ynu.springbootappdemo.controller;

import com.ynu.springbootappdemo.common.utils.BaseResponse;
import com.ynu.springbootappdemo.common.utils.ResultUtils;
import com.ynu.springbootappdemo.model.entity.Goods;
import com.ynu.springbootappdemo.model.entity.Properties;
import com.ynu.springbootappdemo.model.entity.SkuItem;
import com.ynu.springbootappdemo.model.vo.GoodsDetailVo;
import com.ynu.springbootappdemo.model.vo.GoodsVo;
import com.ynu.springbootappdemo.model.vo.SkuItemVo;
import com.ynu.springbootappdemo.service.GoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.modelmapper.ModelMapper;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Api(tags = "商品")
@RestController
@RequestMapping("/goods")
public class GoodsController {
    @Resource
    private GoodsService service;

    @Resource
    private ModelMapper modelMapper;

    @GetMapping("/getAll")
    @ApiOperation("获取首页商品列表")
    public BaseResponse<List<GoodsVo>> getAll() {

        List<GoodsVo> goodsVoList = service.getAllGoods().stream().map(
                goods -> modelMapper.map(goods, GoodsVo.class)).collect(Collectors.toList());

        return ResultUtils.success(goodsVoList);

    }

    @GetMapping("/getById/{id}")
    @ApiOperation("获取商品详情")
    @Transactional
    public BaseResponse<GoodsDetailVo> getById(@PathVariable("id") Long id){

        Goods goods = service.getGoodsById(id);
        if (!Objects.isNull(goods)){
            //获取轮播图
            String mainPictures = goods.getMainPictures();
            List<String> mainPicturesList = Arrays.asList(mainPictures.replace("[", "").replace("]",
                    "").split(", "));
            //数据解析
            List<Properties> properties = service.parserProp(goods.getProperties());
            GoodsDetailVo detailVo = modelMapper.map(goods, GoodsDetailVo.class);

            detailVo.setMainPictures(mainPicturesList);
            detailVo.setProperties(properties);

            //sku
            List<SkuItemVo> skuVoList = new ArrayList<>();
            goods.getSkuItems().forEach(skuItem -> {
                SkuItemVo skuItemVo = modelMapper.map(skuItem, SkuItemVo.class);
                skuItemVo.setItemSpec(service.parserProp(skuItem.getItemSpec()));
                skuVoList.add(skuItemVo);
            });

            detailVo.setSkus(skuVoList);
            return  ResultUtils.success(detailVo);
        }
        return ResultUtils.success(null);
    }

    @GetMapping("/getSkuById/{id}")
    @ApiOperation("获取sku信息")
    @Transactional
    public BaseResponse<SkuItemVo> getSkuById(@PathVariable("id") Long id){
        SkuItem sku = service.getSkuById(id);
        List<Properties> properties = service.parserProp(sku.getItemSpec());

        SkuItemVo vo = modelMapper.map(sku, SkuItemVo.class);
        vo.setItemSpec(properties);

        return ResultUtils.success(vo);
    }
}
